package ccnt.pvc.scudwaremobile.service;

import java.util.List;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import ccnt.pvc.scudwaremobile.dao.ApkModel;
import ccnt.pvc.scudwaremobile.dao.ServiceModel;
import ccnt.pvc.scudwaremobile.storage.ApkDbManager;
import ccnt.pvc.scudwaremobile.storage.ServiceDbManager;

public class ApkService extends Service {

	private final static String TAG = ApkService.class.getName();
	private ApkDbManager apkDbManager = null;
	private ServiceDbManager serviceDbManager = null;
	private InstallReceiver receiver = null;
	
	@Override
	public void onCreate() {
		Log.d(TAG, "onCreate: ApkService");
		super.onCreate();
		
		apkDbManager = new ApkDbManager(ApkService.this);
		serviceDbManager = new ServiceDbManager(ApkService.this);
		
		//注册监听apk安装卸载事件的广播接收者
		receiver = new InstallReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("android.intent.action.PACKAGE_ADDED");
		filter.addAction("android.intent.action.PACKAGE_REMOVED");
		filter.addDataScheme("package");
		registerReceiver(receiver, filter);
	}
	
	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy: ApkService");
		//注销广播接收者
		unregisterReceiver(receiver);
		super.onDestroy();
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private void handleInstall(String packageName) {
		apkDbManager.setIsInstalled(packageName, 1);
		// TODO 注册服务
		
		List<ServiceModel> services = serviceDbManager.getServicesByPackageName(packageName);
		int num = serviceDbManager.insertServicesToReg(services);
		Log.d(TAG, "insert services " + num);
	}
	
	private void handleUninstall(String packageName) {
		apkDbManager.setIsInstalled(packageName, 0);
		// TODO 注销服务
		
		int num = serviceDbManager.deleteRegServiceByPackageName(packageName);
		Log.d(TAG, "delete services " + num);
	}

	/**
	 * 监听apk安装和卸载的reveiver
	 * @author dy
	 *
	 */
	class InstallReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals("android.intent.action.PACKAGE_ADDED")
					|| action.equals("android.intent.action.PACKAGE_REMOVED")) {
				String dataString = intent.getDataString();
				Log.d(TAG, dataString);
				//e.g. package:ccnt.pvc.scudewaremobile.gryoprobe
				String packageName = dataString.split(":")[1];
				
				ApkModel apkModel = apkDbManager.getApkByPackageName(packageName);
				if(apkModel == null) {
					Log.d(TAG, packageName + " is not for scudware"); 
					return;
				}
				
				if(action.equals("android.intent.action.PACKAGE_ADDED")) {
					Log.d(TAG, "apk installed: " + packageName);
					handleInstall(packageName);
				} else {
					Log.d(TAG, "apk removeed: " + packageName);
					handleUninstall(packageName);
				}
			}
		}
		
	}
}
